package niuke;

public class Test02 {
    public static class WaitThread {
        private Object locker = null;

        public WaitThread(Object lock) {
            this.locker = lock;
        }

        Thread t1 = new Thread() {
            //使用wait使该 线程进入等待状态
            @Override
            public void run() {
                synchronized (locker) {
                    System.out.println("whit 开始");
                    try {
                        locker.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println("whit 结束");
                }
            }
        };
    }

    public static class NotifyThread {
        private Object locker = null;
        public NotifyThread(Object locker) {
            this.locker =locker;
        }
        //使用notify唤醒等待线程
        Thread t2 = new Thread() {
            @Override
            public void run() {
                synchronized (locker) {
                    System.out.println("notify 开始");
                    locker.notify();
                    System.out.println("notify 结束");
                }
            }
        };
    }

    public static void main(String[] args) throws InterruptedException {
        //此处创建一个 lock对象，传给两个线程，保证两个线程中wait 和 notify 调用的是同一个对象
        Object lock = new Object();
        WaitThread t1 = new WaitThread(lock);
        NotifyThread t2 =new NotifyThread(lock);
        t1.t1.start();
        Thread.sleep(3000);
        t2.t2.start();
    }
}